//try this with NLSY79


import delimited "$data/NLSY79/nlsy79_emp.csv", clear
ren *, upper
do "$data/NLSY79/nlsy79_emp-value-labels"
ren *, lower

ren caseid uniqid
ren sample_id sample_id
ren sample_race race
ren sample_sex sex

forval y = 1979/2021{
	cap ren sampweight_`y' weight_`y'
	cap ren c_sampweight_`y' weight_c_`y'
	cap ren tnfi_trunc_`y' faminc_`y'
	cap ren wksuemp_sli_`y' unemp_`y'
	cap ren q13_5_trunc_revised_`y' inc_`y'
	cap ren q13_5_trunc_`y' inc_`y'
}

//missing codes
ds 
foreach var in `r(varlist)'{
	replace `var' = . if `var' == -1
	replace `var' = . if `var' == -2
	replace `var' = . if `var' == -3
	replace `var' = . if `var' == -4
	replace `var' = . if `var' == -5	
}

//only keep the baseline cross-sectional sample
keep if sample_id<9

//now let's get some info on father layoffs in the recession
keep if sex == 1
drop faminc*

su inc_2000 inc_2002 inc_2004 inc_2006 inc_2008

//adjust income measures to be real
replace inc_2000 = inc_2000 / (0.78235)
replace inc_2002 = inc_2002 / (0.80789)
replace inc_2004 = inc_2004 / (0.84411)
replace inc_2006 = inc_2006 / (0.89174)
replace inc_2008 = inc_2008 / (0.9418)
replace inc_2010 = inc_2010 / (0.95705)




//pre-recession income. Kill missings
egen inc_pre_rec = rowmean(inc_2006 inc_2008)
egen inc_post_rec = rowmean(inc_2010 inc_2012)
gen inc_rat = inc_post_rec / inc_pre_rec
drop if inc_pre_rec == .
xtile inc_pre_rec_dec = inc_pre_rec [w=weight_2000], nq(10) //income decile
xtile inc_pre_rec_vent = inc_pre_rec [w=weight_2000], nq(20) //income ventile

gen layoff_rec = 0
replace layoff_rec = 1 if (unemp_2010>13 & unemp_2010<.)
replace layoff_rec = 1 if (unemp_2012>13 & unemp_2012<.)

gen layoff_pre = 0
replace layoff_pre = 1 if (unemp_2006>13 & unemp_2006<.)
replace layoff_pre = 1 if (unemp_2008>13 & unemp_2008<.)


//replace layoff_rec = 1 if (unemp_2014>13 & unemp_2014<.)
su layoff_rec [w=weight_2000]

qui{
forval i = 1/10{
	su layoff_rec [w=weight_2000] if inc_pre_rec_dec == `i'
	
	
	su inc_pre_rec[w=weight_2000] if inc_pre_rec_dec == `i' & layoff_rec
	local inc_pre = `r(mean)'
	
	su inc_post_rec[w=weight_2000] if inc_pre_rec_dec == `i' & layoff_rec
	local inc_post = `r(mean)'
	
	
	su inc_rat [w=weight_2000] if layoff_rec & inc_pre_rec_dec == `i', d
	local frac_loss_`i' = `r(p50)'
	//local frac_loss_`i' = `inc_post'/`inc_pre'
	noi di "`frac_loss_`i''"
}
}

forval i = 1/10{
	su layoff_rec [w=weight_2000] if inc_pre_rec_dec == `i'
	local step1 = `r(mean)'
	
	su layoff_pre [w=weight_2000] if inc_pre_rec_dec == `i'
	local step2 = `r(mean)'
	
	
	local layoff_`i' = `step1' - `step2'
}

clear
set obs 20
gen layoff_prob = .
gen layoff_shock = .
forval i = 1/10{
	local r1 = `i' * 2 - 1
	local r2 = `i' * 2
	
	replace layoff_prob = `layoff_`i'' in `r1'
	replace layoff_prob = `layoff_`i'' in `r2'
}


forval j = 1/10{
	local r1 = `j' * 2 - 1
	local r2 = `j' * 2
	replace layoff_shock = `frac_loss_`j'' in `r1'
	replace layoff_shock = `frac_loss_`j'' in `r2'
}


gen ventile = _n
reg layoff_prob c.ventile##c.ventile##c.ventile
predict prob
export delimited "$dir/model/utilities/rec_layoff_probs.csv", replace novarn




//end of dofile